const elastic = require('./elastic');
const latitudeHelper = require('./latitudeHelper');
//const loopback = require("./loopback");

class analysis {
  constructor(rulePath, wd_TOOLS) {
    this.wd_TOOLS = wd_TOOLS;
    this.rulePath = rulePath;
    this.rule = require(this.rulePath);
    this.count = 0;
    if (!this.rule) return;
    this.get_rule_data_list();
  }
  
  get_rule_data_list() {
    let self = this;
    self.es = new elastic(global.es.source.ip, global.es.source.verison);
    let one = self.rule.唯一标识来源[0];
    self.es.scroll({index: one.index, type: one.type, fields: [one.field]}, function (data) {
      if (data && data.length > 0) {
        (function next(i) {
          if (i >= data.length) return;
          self.analysis_one_data(data[i], one.field, next, i + 1);
        })(0);
        return false;   //  不结束
      } else {
        return true;    //  结束
      }
    });
  }
  
  analysis_one_data(hit, field, callback, i) {
    try {
      let self = this;
      this.count++;
      console.log(this.count);
      let key = hit.fields[field];
      if (Array.isArray(key)) key = key[0];
      latitudeHelper.PutLatitude({EntityName: "wd_AJBH", value: key, index: hit._index, type: hit._type, id: hit._id}, function (err, obj) {
        try {
          if (err || !obj.success) return callback(i);
          global.App.models["wd_AJBH"].find({where:{"id":key}, include:["ajbh-xzaj","ajbh-xsaj","ajbh-xxr","ajbh-bar","ajbh-zr","ajbh-bhr"]}, function (err, models) {
            self.assemble_data(models, key);
          });
        } catch (e) {}
        callback(i);
      });
    } catch (e) {
      callback(i);
    }
  }
  
  assemble_data (models, id){
    let self = this;
    let objects = {id: id, version:1};
    self.rule.集合.forEach(function (rule) {
      objects[rule.名称] = [];
      
      
      
      for (let field of rule.字段){
        
        
      }
      
      
      
      
    });
    
    
  }
}

module.exports = analysis;
